home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MacDesktopIconUI.java < prev    next >
Text File  |  1998-06-30  |  13KB  |  380 lines

  1. /*
  2.  * @(#)MacDesktopIconUI.java    1.8 98/02/05
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21.  
  22. package com.sun.java.swing.plaf.mac;
  23.  
  24. import java.awt.*;
  25. import java.awt.event.*;
  26. import com.sun.java.swing.*;
  27. import com.sun.java.swing.border.*;
  28. import com.sun.java.swing.plaf.*;
  29. import java.beans.*;
  30. import java.util.EventListener;
  31. import java.io.Serializable;
  32.  
  33. /**
  34.  * <p>
  35.  * Warning: serialized objects of this class will not be compatible with
  36.  * future swing releases.  The current serialization support is appropriate
  37.  * for short term storage or RMI between Swing1.0 applications.  It will
  38.  * not be possible to load serialized Swing1.0 objects with future releases
  39.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  40.  * baseline for the serialized form of Swing objects.
  41.  *
  42.  * @version @(#)MacDesktopIconUI.java    1.0 11/24/97
  43.  * @author Symantec
  44.  */
  45. public class MacDesktopIconUI extends DesktopIconUI implements Serializable
  46. {
  47.     JInternalFrame.JDesktopIcon desktopIcon;
  48.     JInternalFrame frame;
  49.     Icon defaultIcon;
  50.     IconButton iconButton;
  51.     IconLabel iconLabel;
  52.     EventListener mml;
  53.  
  54.     final static Font defaultTitleFont = new Font("SansSerif", Font.PLAIN, 12);
  55.  
  56.     public static ComponentUI createUI(JComponent c)    {
  57.         return new MacDesktopIconUI();
  58.     }
  59.  
  60.     public MacDesktopIconUI() {
  61.     }
  62.  
  63.     public void installUI(JComponent c)   {
  64.     desktopIcon = (JInternalFrame.JDesktopIcon)c;
  65.     frame = desktopIcon.getInternalFrame();
  66.         setDefaultIcon(UIManager.getIcon("DesktopIcon.icon"));
  67.         iconButton = new IconButton(defaultIcon);
  68.  
  69.         // An unhanded way of creating a system popup menu.
  70.     MacInternalFrameTitlePane titlePane = 
  71.             new MacInternalFrameTitlePane(frame);
  72.  
  73.     iconButton.addActionListener(new ActionListener() {
  74.             public void actionPerformed(ActionEvent e) {
  75.             }
  76.         });
  77.         iconButton.addMouseListener(new MouseAdapter() {
  78.             public void mousePressed(MouseEvent e) {
  79.                 if (e.getClickCount() == 2) {
  80.                     try {
  81.                         frame.setIcon(false);
  82.                     } catch (PropertyVetoException e2) { }
  83.                 }
  84.             }
  85.         });
  86.  
  87.         MacFrameBorder border = new MacFrameBorder(desktopIcon);
  88.     desktopIcon.setLayout(new BorderLayout());
  89.         iconButton.setBorder(border);
  90.     desktopIcon.add(iconButton, BorderLayout.CENTER);
  91.         iconLabel = new IconLabel();
  92.         iconLabel.setBorder(border);
  93.         desktopIcon.add(iconLabel, BorderLayout.SOUTH);
  94.         desktopIcon.setSize(desktopIcon.getPreferredSize());
  95.         desktopIcon.validate();
  96.  
  97.     mml = new MotionListener();
  98.     desktopIcon.addMouseMotionListener((MouseMotionListener)mml);
  99.     desktopIcon.addMouseListener((MouseListener)mml);
  100.     JLayeredPane.putLayer(desktopIcon, JLayeredPane.getLayer(frame));
  101.     }
  102.  
  103.     public void uninstallUI(JComponent c) {
  104.     desktopIcon.setLayout(null);
  105.     desktopIcon.remove(iconButton);
  106.     desktopIcon.remove(iconLabel);
  107.     desktopIcon.removeMouseMotionListener((MouseMotionListener)mml);
  108.     desktopIcon.removeMouseListener((MouseListener)mml);
  109.     desktopIcon = null;
  110.     frame = null;
  111.     }
  112.  
  113.     final static int LABEL_HEIGHT = 18;
  114.     final static int LABEL_DIVIDER = 4;    // padding between icon and label
  115.  
  116.     public Dimension getMinimumSize(JComponent c) {
  117.         JInternalFrame iframe = desktopIcon.getInternalFrame();
  118.         int w = defaultIcon.getIconWidth();
  119.         int h = defaultIcon.getIconHeight() + LABEL_HEIGHT + LABEL_DIVIDER;
  120.  
  121.     Border border = iframe.getBorder();
  122.     if(border != null) {
  123.         w += border.getBorderInsets(iframe).left + 
  124.                 border.getBorderInsets(iframe).right;
  125.         h += border.getBorderInsets(iframe).bottom + 
  126.                 border.getBorderInsets(iframe).top;
  127.         }
  128.  
  129.     return new Dimension(w, h);
  130.     }
  131.  
  132.     public Dimension getPreferredSize(JComponent c) {
  133.         return getMinimumSize(c);
  134.     }
  135.  
  136.     public Dimension getMaximumSize(JComponent c){
  137.         return getMinimumSize(c);
  138.     }
  139.  
  140.     public Insets getInsets(JComponent c) {
  141.         JInternalFrame iframe = desktopIcon.getInternalFrame();
  142.     Border border = iframe.getBorder();
  143.     if(border != null)
  144.         return border.getBorderInsets(iframe);
  145.     
  146.     return new Insets(0,0,0,0);
  147.     }
  148.  
  149.     private class MotionListener 
  150.         extends MouseAdapter implements MouseMotionListener, Serializable
  151.     {
  152.     // _x & _y are the mousePressed location in absolute coordinate system
  153.         int _x, _y;
  154.     // __x & __y are the mousePressed location in source view's coordinate system
  155.     int __x, __y;
  156.         Rectangle startingBounds;
  157.  
  158.         public void mouseReleased(MouseEvent e) {
  159.             _x = 0;
  160.             _y = 0;
  161.             __x = 0;
  162.             __y = 0;
  163.             startingBounds = null;
  164.         }
  165.                 
  166.         public void mousePressed(MouseEvent e) {
  167.             Point p = SwingUtilities.convertPoint((Component)e.getSource(), 
  168.                         e.getX(), e.getY(), null);
  169.             __x = e.getX();
  170.             __y = e.getY();
  171.             _x = p.x;
  172.             _y = p.y;
  173.             startingBounds = desktopIcon.getBounds();
  174.  
  175.             try { frame.setSelected(true); } catch (PropertyVetoException e1) { }
  176.         if(desktopIcon.getParent() instanceof JLayeredPane) {
  177.         ((JLayeredPane)desktopIcon.getParent()).moveToFront(desktopIcon);
  178.          }
  179.  
  180.             if(e.getClickCount() > 1) {
  181.         if(frame.isIconifiable() && frame.isIcon()) {
  182.                     try { frame.setIcon(false); } catch (PropertyVetoException e2) { }
  183.         }
  184.             }
  185.  
  186.      }
  187.  
  188.          public void mouseMoved(MouseEvent e) {}
  189.  
  190.          public void mouseDragged(MouseEvent e) {                                        
  191.             Point p; 
  192.         int newX, newY, newW, newH;
  193.             int deltaX;
  194.             int deltaY;
  195.         Dimension min;
  196.         Dimension max;
  197.             p = SwingUtilities.convertPoint((Component)e.getSource(), 
  198.                                         e.getX(), e.getY(), null);
  199.         
  200.         Insets i = desktopIcon.getInsets();
  201.         int pWidth, pHeight;
  202.         pWidth = ((JComponent)desktopIcon.getParent()).getWidth();
  203.         pHeight = ((JComponent)desktopIcon.getParent()).getHeight();
  204.         
  205.         newX = startingBounds.x - (_x - p.x);
  206.         newY = startingBounds.y - (_y - p.y);
  207.         // Make sure we stay in-bounds
  208.         if(newX + i.left <= -__x)
  209.             newX = -__x - i.left;
  210.         if(newY + i.top <= -__y)
  211.             newY = -__y - i.top;
  212.         if(newX + __x + i.right > pWidth)
  213.             newX = pWidth - __x - i.right;
  214.         if(newY + __y + i.bottom > pHeight)
  215.             newY =  pHeight - __y - i.bottom;
  216.         
  217.         JDesktopPane d;
  218.         if((d = desktopIcon.getDesktopPane()) != null) {
  219.             DesktopManager dm;
  220.             dm = d.getDesktopManager();
  221.             dm.setBoundsForFrame(desktopIcon, newX, newY, 
  222.                          desktopIcon.getWidth(), 
  223.                          desktopIcon.getHeight());
  224.         } else {
  225.             moveAndRepaint(desktopIcon, newX, newY, 
  226.                 desktopIcon.getWidth(), desktopIcon.getHeight());
  227.         }
  228.         return;
  229.     }
  230.  
  231.         public void moveAndRepaint(JComponent f, int newX, int newY, 
  232.                     int newWidth, int newHeight) {
  233.         Rectangle r = f.getBounds();
  234.         f.setBounds(newX, newY, newWidth, newHeight);        
  235.         SwingUtilities.computeUnion(newX, newY, newWidth, newHeight, r);
  236.         f.getParent().repaint(r.x, r.y, r.width, r.height);
  237.         }    
  238.     }; /// End MotionListener
  239.  
  240.     /**
  241.       * Returns the default desktop icon.
  242.       */
  243.     public Icon getDefaultIcon() {
  244.     return defaultIcon;
  245.     }
  246.  
  247.     /**
  248.       * Sets the icon used as the default desktop icon.
  249.       */
  250.     public void setDefaultIcon(Icon newIcon) {
  251.     defaultIcon = newIcon;
  252.     }
  253.  
  254.     class IconLabel extends JPanel {
  255.  
  256.         IconLabel() {
  257.             super();
  258.             setFont(defaultTitleFont);
  259.  
  260.             // Forward mouse events to titlebar for moves.
  261.             addMouseMotionListener(new MouseMotionListener() {
  262.                 public void mouseDragged(MouseEvent e) {
  263.                     forwardEventToParent(e);
  264.                 }
  265.                 public void mouseMoved(MouseEvent e) {
  266.                     forwardEventToParent(e);
  267.                 }
  268.             });
  269.             addMouseListener(new MouseListener() {
  270.                 public void mouseClicked(MouseEvent e) {
  271.                     forwardEventToParent(e);
  272.                 }
  273.                 public void mousePressed(MouseEvent e) {
  274.                     forwardEventToParent(e);
  275.                 }
  276.                 public void mouseReleased(MouseEvent e) {
  277.                     forwardEventToParent(e);
  278.                 }
  279.                 public void mouseEntered(MouseEvent e) {
  280.                     forwardEventToParent(e);
  281.                 }
  282.                 public void mouseExited(MouseEvent e) {
  283.                     forwardEventToParent(e);
  284.                 }
  285.             });
  286.         }
  287.  
  288.         void forwardEventToParent(MouseEvent e) {
  289.             getParent().dispatchEvent(new MouseEvent(
  290.                 getParent(), e.getID(), e.getWhen(), e.getModifiers(),
  291.                 e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger()));
  292.         }
  293.  
  294.         public boolean isFocusTraversable() { 
  295.             return false; 
  296.         }
  297.  
  298.         public Dimension getMinimumSize() {
  299.             return new Dimension(defaultIcon.getIconWidth() + 1,
  300.                                  LABEL_HEIGHT + LABEL_DIVIDER);
  301.         }
  302.  
  303.         public Dimension getPreferredSize() {
  304.             String title = frame.getTitle();
  305.             FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(defaultTitleFont);
  306.             int w = fm.stringWidth(title) + 4;
  307.             return new Dimension(w, LABEL_HEIGHT + LABEL_DIVIDER);
  308.         }
  309.  
  310.         public void paint(Graphics g) {
  311.             super.paint(g);
  312.  
  313.             // touch-up frame
  314.             int maxX = getWidth() - 1;
  315.             Color shadow = 
  316.                 UIManager.getColor("inactiveCaptionBorder").darker().darker();
  317.             g.setColor(shadow);
  318.             g.setClip(0, 0, getWidth(), getHeight());
  319.             g.drawLine(maxX - 1, 1, maxX - 1, 1);
  320.             g.drawLine(maxX, 0, maxX, 0);
  321.  
  322.             // fill background
  323.             g.setColor(UIManager.getColor("inactiveCaption"));
  324.             g.fillRect(2, 1, maxX - 3, LABEL_HEIGHT + 1);
  325.  
  326.             // draw text -- clipping to truncate text like CDE/Mac
  327.             g.setClip(2, 1, maxX - 4, LABEL_HEIGHT);
  328.             int y = LABEL_HEIGHT - g.getFontMetrics().getDescent();
  329.             g.setColor(UIManager.getColor("inactiveCaptionText"));
  330.             g.drawString(frame.getTitle(), 4, y);
  331.         }
  332.     }
  333.  
  334.     class IconButton extends JButton {
  335.         Icon icon;
  336.  
  337.         IconButton(Icon icon) {
  338.             super(icon);
  339.             this.icon = icon;
  340.  
  341.             // Forward mouse events to titlebar for moves.
  342.             addMouseMotionListener(new MouseMotionListener() {
  343.                 public void mouseDragged(MouseEvent e) {
  344.                     forwardEventToParent(e);
  345.                 }
  346.                 public void mouseMoved(MouseEvent e) {
  347.                     forwardEventToParent(e);
  348.                 }
  349.             });
  350.             addMouseListener(new MouseListener() {
  351.                 public void mouseClicked(MouseEvent e) {
  352.                     forwardEventToParent(e);
  353.                 }
  354.                 public void mousePressed(MouseEvent e) {
  355.                     forwardEventToParent(e);
  356.                 }
  357.                 public void mouseReleased(MouseEvent e) {
  358.                     forwardEventToParent(e);
  359.                 }
  360.                 public void mouseEntered(MouseEvent e) {
  361.                     forwardEventToParent(e);
  362.                 }
  363.                 public void mouseExited(MouseEvent e) {
  364.                     forwardEventToParent(e);
  365.                 }
  366.             });
  367.         }
  368.  
  369.         void forwardEventToParent(MouseEvent e) {
  370.             getParent().dispatchEvent(new MouseEvent(
  371.                 getParent(), e.getID(), e.getWhen(), e.getModifiers(),
  372.                 e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger()));
  373.         }
  374.  
  375.         public boolean isFocusTraversable() { 
  376.             return false; 
  377.         }
  378.     }
  379. }
  380.